﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;

public class AStarManager : SingletonManager<AStarManager>
{
    [Header("地图")]
    public int mapW;
    public int mapH;
    public AStarNode[,] maps;
    [Header("子AStar列表")]
    public List<AStar> aStars = new List<AStar>();

    //初始化
    public void Init(int _mapW,int _mapH)
    {
        mapH=_mapH;
        mapW=_mapW;
        maps=new AStarNode[mapW,mapH];
        for(int i=0;i<mapW;i++)
        {
            for(int j=0;j<mapH;j++)
            {
                maps[i,j]=new AStarNode();
                maps[i, j].pos=new Vector2Int(i, j);
            }
        }
        //赋值所有
        SetAllAStar();
    }
    //添加子AStar
    public void AddAStar(AStar aStar)
    {
        if(!aStars.Contains(aStar))
        {
            aStars.Add(aStar);
            //赋值所有
            SetAllAStar();
        }
    }
    //移除子AStar
    public void ReAStar(AStar aStar)
    {
        if(aStars.Contains(aStar))
        {
            aStars.Remove(aStar);
        }
    }
    //赋值所有子AStar
    public void SetAllAStar()
    {
        foreach(var i in aStars)
        {
            i.Init(maps, mapW, mapH);
        }
    }
    //设置障碍信息
    public void SetStop(int x,int y)
    {
        SetNodeType(x, y, false);
    }
    //设置可以行走信息
    public void SetRun(int x,int y)
    {
        SetNodeType(x, y, true);
    }
    //设置敌人信息
    public void SetEnemy(int x, int y)
    {
        //越界判断
        if (x < 0 || x >= mapW || y < 0 || y >= mapH)
        {
            Debug.Log("【" + x + "," + y + "】坐标越界");
            return;
        }
        maps[x, y].type = NodeType.Enemy;
    }
    //设置权重
    public void SetWeight(int x, int y, float weight)
    {
        //判断越界
        if (x<0||x>=mapW||y<0||y>=mapH)
        {
            Debug.Log("【"+x+","+y+"】坐标越界");
            return;
        }
        //设置权重
        maps[x, y].weight=weight;
    }
    //设置网格类型
    public void SetNodeType(int x, int y, bool isRun)
    {
        //越界判断
        if (x<0||x>=mapW||y<0||y>=mapH)
        {
            Debug.Log("【"+x+","+y+"】坐标越界");
            return;
        }
        if (isRun)
        {
            maps[x, y].type=NodeType.Run;
        }
        else
        {
            maps[x, y].type=NodeType.Stop;
        }
    }
}